<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Models\WalletLog;
use App\Models\WeixinUser;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
class WalletLogController extends Controller
{
    public function index(Request $request){
        $rules = [
            'token'=>'string',
            'type'=>'integer|in:0,1'
        ];
        $message = [
            'token.*'=>'请传递令牌',
            'type.*'=>'请选择正确的消费类型'
        ];
        $validator = Validator::make($request->all(), $rules,$message);
        if ($validator->fails()) {
            return show(201,$validator->errors()->first());
        }
        try{
            $token = $request->token;
            if(!$token){//auth验证
                if(!Auth::id())
                {
                    return  redirect('/login');
                }
                $user_id = Auth::id();
            }else{//token验证
                $WeixinUser = WeixinUser::where('token',$token)->first();
                if(!$WeixinUser){
                    return show("203","无效token！");
                }else{
                    $user= User::find($WeixinUser->user_id);
                    if(!$user){
                        return show("203","用户不存在！");
                    }
                }
                $user_id = $WeixinUser->user_id;
            }
//            select * from `orders` where `orders`.`order_id` in (4, 5, 6, 7) and `orders`.`deleted_at` is null)"
            $perPage = 15;
            if (isset($request->type)){
                $res = WalletLog::where('user_id',$user_id)->where('type',$request->type);
            }else{
                $res = WalletLog::where('user_id',$user_id);
            }
            $res = $res->orderBy('created_at','desc')->paginate($perPage,['id','serial_number','type','work_capital','sign','created_at']);
            if ($res->isEmpty()){
                return show(202,'暂无明细');
            }
        }catch (\Exception $e){
            //dd($e->getMessage());
            return show(202,'暂无明细');
        }
        return show(200,'明细信息',$res);
    }

    public function read(Request $request){
        $rules = [
            'id'=>'required|integer|exists:wallet_logs,id',
            'token'=>'string'
        ];
        $message = [
            'id.*'=>'请选择正确的明细编号',
            'token.*'=>'请传递令牌',
        ];
        $validator = Validator::make($request->all(), $rules,$message);
        if ($validator->fails()) {
            return show(201,$validator->errors()->first());
        }
        $token = $request->token;
        if(!$token){//auth验证
            if(!Auth::id())
            {
                return  redirect('/login');
            }
            $user_id = Auth::id();
        }else{//token验证
            $WeixinUser = WeixinUser::where('token',$token)->first();
            if(!$WeixinUser){
                return show("201","无效token！");
            }else{
                $user= User::find($WeixinUser->user_id);
                if(!$user){
                    return show("203","用户不存在！");
                }
            }
            $user_id = $WeixinUser->user_id;
        }

        $walletlog = WalletLog::where('user_id',$user_id)->find($request->id);

        if (!$walletlog){
            return show(202,'不存在该明细信息');
        }

        return show(200,'明细信息详情',$walletlog);
    }
}
